package com.chen.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.dto.AutoAddSubjectDTO;
import com.chen.dto.ExamPaperDTO;
import com.chen.entity.ExamPaper;
import com.chen.entity.ExamPaperConnectSubject;
import com.chen.mapper.*;
import com.chen.service.ExamPaperService;
import com.chen.vo.ExamPaperVO;
import com.chen.vo.PapersDataVO;
import com.chen.vo.SubjectVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 试卷相关服务实现
 */
@Service
public class ExamPaperServiceImpl extends ServiceImpl<ExamPaperMapper, ExamPaper> implements ExamPaperService {

    // 考试计划
    @Autowired
    private ExamPlanMapper examPlanMapper;

    // 考试记录
    @Autowired
    private ExamHistoryMapper examHistoryMapper;

    // 答题记录
    @Autowired
    private ExamChooseMapper examChooseMapper;

    // 中间表
    @Autowired
    private ExamPaperConnectSubjectMapper examPaperConnectSubjectMapper;

    // 试题
    @Autowired
    private SubjectMapper subjectMapper;



    @Override
    public Long findAddCount() {
        return baseMapper.findAllCount();
    }

    /**
     * 分页查询试卷信息
     * @param gradeId
     * @param index
     * @param pageShow
     * @return
     */
    @Override
    public List<ExamPaperVO> listPage(Integer gradeId, Integer index, Integer pageShow) {
        return baseMapper.listPage(gradeId, index, pageShow);
    }

    @Override
    public List<ExamPaperVO> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public ExamPaperVO findById(Integer id) {
        return baseMapper.findById(id);
    }

    /**
     * 新增试卷
     * @param dto
     * @return
     */
    @Override
    public Integer saveExamPaper(ExamPaperDTO dto) {
        ExamPaper examPaper = BeanUtil.copyProperties(dto, ExamPaper.class);
        return baseMapper.insert(examPaper);
    }

    @Override
    public Integer updateExamPaper(ExamPaperDTO dto) {
        Integer id = dto.getId();
        Assert.isTrue(ObjectUtil.isNotEmpty(id), "试卷id不能为空");
        ExamPaper examPaper = BeanUtil.copyProperties(dto, ExamPaper.class);
        return baseMapper.updateById(examPaper);
    }

    @Override
    @Transactional
    public Integer delExamPaperId(Integer examPaperId) {
        examPlanMapper.deleteByExamPaperId(examPaperId);
        examHistoryMapper.deleteByExamPaperId(examPaperId);
        examChooseMapper.deleteByExamPaperId(examPaperId);
        examPaperConnectSubjectMapper.deleteByExamPaperId(examPaperId);

        return baseMapper.deleteById(examPaperId);
    }

    /**
     * 自动对试题添加题目
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Integer autoAddSubject(AutoAddSubjectDTO dto) {
        // 试卷id
        Integer id = dto.getId();
        // 试题个数
        Integer subjectSum = dto.getSubjectSum();
        // 课程id
        Integer courseId = dto.getCourseId();
        // 年级id
        Integer gradeId = dto.getGradeId();
        // 难度
        Integer easy = dto.getEasy();
        // 参数不全则不做处理
        if (ObjectUtil.isEmpty(id) || ObjectUtil.isEmpty(subjectSum) || ObjectUtil.isEmpty(courseId) || ObjectUtil.isEmpty(gradeId) || ObjectUtil.isEmpty(easy)) {
            return 0;
        }

        Integer bindSubjectCount = 0;
        // 总题目分数
        Integer totalScore = 0;
        // 获取该试卷未被绑定的题
        List<SubjectVO> subjects = subjectMapper.randomNotExamPaper(id, courseId, gradeId, easy, subjectSum);
        if (CollectionUtil.isNotEmpty(subjects)) {
            for (SubjectVO subject : subjects) {
                totalScore += subject.getScore();
                ExamPaperConnectSubject examPaperConnectSubject = ExamPaperConnectSubject.builder().subjectId(subject.getId()).examPaperId(id).build();
                examPaperConnectSubjectMapper.insert(examPaperConnectSubject);
                bindSubjectCount ++;
            }
            // 添加题目分数
            baseMapper.addScoreById(id, totalScore);
            return bindSubjectCount;
        }
        return 0;
    }

    @Override
    public List<PapersDataVO> getExamPapersData(Integer teacherId) {
        return baseMapper.getExamPapersData(teacherId);
    }
}
